
LIBRARY ieee;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_ARITH.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
USE ieee.std_logic_textio.all;

USE work.types.all;
USE work.mem_file.all;

ENTITY rom IS
    PORT
    (
        address  : IN   MEMADDR;
        clock    : IN   STD_LOGIC := '1';
        q        : OUT  INSTRUCTION
    );
END rom;


ARCHITECTURE sync OF rom IS
    --SUBTYPE sword IS STD_LOGIC_VECTOR(7 DOWNTO 0);
    --TYPE word_array IS ARRAY (0 to 255) of sword;

    CONSTANT word_list: word_array := READ_MEMORY_FILE("program.txt");
--    SIGNAL address_aux     : MEMADDR   := (OTHERS => '0');
--    SIGNAL count    : BIT       := '0';
BEGIN

    PROCESS (clock)
        --FILE input          : TEXT OPEN READ_MODE IS "program.dat";
        --VARIABLE input_line : LINE;
        --VARIABLE input_addr : STD_LOGIC_VECTOR(7 DOWNTO 0);
        --VARIABLE input_data : STD_LOGIC_VECTOR(31 DOWNTO 0);
		  --VARIABLE lines	: INTEGER RANGE 0 TO 127 := 0;
    BEGIN
        IF rising_edge(clock) THEN
            --IF count = '0' THEN
            --    count <= '1';
                --FILE_OPEN(input, "program.dat", Read_Mode);
            --    WHILE lines < 128 AND NOT ENDFILE(input) LOOP
            --        READLINE( input, input_line );
            --        READ( input_line, input_addr );
            --        READ( input_line, input_data );

            --        word_list( CONV_INTEGER( input_addr )*4+0 ) <= input_data(31 DOWNTO 24);
				--		  word_list( CONV_INTEGER( input_addr )*4+1 ) <= input_data(23 DOWNTO 16);
				--		  word_list( CONV_INTEGER( input_addr )*4+2 ) <= input_data(15 DOWNTO 8);
				--		  word_list( CONV_INTEGER( input_addr )*4+3 ) <= input_data(7 DOWNTO 0);
						  
				--		  lines := lines + 1;
            --    END LOOP;
            --END IF;

            --address_aux <= address;
            q <= word_list( CONV_INTEGER( address ) ) &
			word_list( CONV_INTEGER( address )+1 ) &
			word_list( CONV_INTEGER( address )+2 ) &
			word_list( CONV_INTEGER( address )+3 );
        END IF;
    END PROCESS;
END sync;
